package ie.dcu.spca.model.dao;

import ie.dcu.spca.model.Bean;
import ie.dcu.spca.model.CommonOperation;
import ie.dcu.spca.model.DBconnection;
import ie.dcu.spca.model.Dao;
import ie.dcu.spca.model.bean.Item;
import ie.dcu.spca.model.sql.itemSQL;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.database.Cursor;

public class ItemDao extends Dao implements CommonOperation {
    private Item cursorToItem(Cursor cursor) {
        cursor.moveToNext();
        int idIndex = cursor.getColumnIndex("id");
        int bill_idIndex = cursor.getColumnIndex("bill_id");
        int product_idIndex = cursor.getColumnIndex("product_id");
        int countIndex = cursor.getColumnIndex("count");
        int creat_atIndex = cursor.getColumnIndex("create_at");
        int update_atIndex = cursor.getColumnIndex("update_at");
        int delete_atIndex = cursor.getColumnIndex("delete_at");
        int id = cursor.getInt(idIndex);
        int bill_id = cursor.getInt(bill_idIndex);
        int product_id = cursor.getInt(product_idIndex);
        double count = cursor.getDouble(countIndex);
        long creat_at = cursor.getLong(creat_atIndex);
        long update_at = cursor.getLong(update_atIndex);
        long delete_at = cursor.getLong(delete_atIndex);
        cursor.close();
        return new Item(id, bill_id, product_id, count, creat_at, update_at,
                delete_at);
    }

    private List<Item> cursorToItemList(Cursor cursor) {
        List<Item> itemList = new ArrayList<Item>();
        while (cursor.moveToNext()) {
            int idIndex = cursor.getColumnIndex("id");
            int bill_idIndex = cursor.getColumnIndex("bill_id");
            int product_idIndex = cursor.getColumnIndex("product_id");
            int countIndex = cursor.getColumnIndex("count");
            int creat_atIndex = cursor.getColumnIndex("create_at");
            int update_atIndex = cursor.getColumnIndex("update_at");
            int delete_atIndex = cursor.getColumnIndex("delete_at");
            int id = cursor.getInt(idIndex);
            int bill_id = cursor.getInt(bill_idIndex);
            int product_id = cursor.getInt(product_idIndex);
            double count = cursor.getDouble(countIndex);
            long creat_at = cursor.getLong(creat_atIndex);
            long update_at = cursor.getLong(update_atIndex);
            long delete_at = cursor.getLong(delete_atIndex);
            itemList.add(new Item(id, bill_id, product_id, count, creat_at,
                    update_at, delete_at));
        }
        cursor.close();
        return itemList;
    }

    public List<Item> selectItemListByBillid(int id) {
        DBconnection conn = DBconnection.getInstance();
        String sql = String.format(itemSQL.selectItemListByBillid, id);
        Cursor cursor = conn.querySql(sql);
        return this.cursorToItemList(cursor);
    }

    public int insertItem(int bill_id, int product_id, double count) {
        DBconnection conn = DBconnection.getInstance();
        int id = this.selectMaxId() + 1;
        Object[] args={ id, count, bill_id,
                product_id, (new Date()).getTime(), 0, 0};
        conn.executeSql(itemSQL.insertItem, args);
        return id;
    }

    @Override
    public Bean selectById(int id) {
        DBconnection conn = DBconnection.getInstance();
        String sql = String.format(itemSQL.selectById, id);
        Cursor cursor = conn.querySql(sql);
        return this.cursorToItem(cursor);
    }

    @Override
    public int insertByBean(Bean bean) {
        DBconnection conn = DBconnection.getInstance();
        Item item = (Item) bean;
        int id = item.getId();
        if (this.existById(id))
            return -1;
        double count = item.getCount();
        int bill_id = item.getBill_id();
        int product_id = item.getProduct_id();
        long create_at = item.getCreate_at().getTime();
        long update_at = item.getUpdate_at().getTime();
        long delete_at = item.getDelete_at().getTime();
        Object[] args={id, count, bill_id,
                product_id, create_at, update_at, delete_at};
        conn.executeSql(itemSQL.insertByBean,args);
        return id;
    }

    @Override
    public boolean deleteById(int id) {
        DBconnection conn = DBconnection.getInstance();
        if (!this.existById(id))
            return false;
        Object[] args={ (new Date()).getTime(),
                id};
        conn.executeSql(itemSQL.deleteById,args);
        return true;
    }

    @Override
    public boolean deleteByBean(Bean bean) {
        return this.deleteById(((Item) bean).getId());
    }

    @Override
    public boolean updateByBean(Bean bean) {
        DBconnection conn = DBconnection.getInstance();
        Item item = (Item) bean;
        int id = item.getId();
        if (!this.existById(id))
            return false;
        double count = item.getCount();
        int bill_id = item.getBill_id();
        int product_id = item.getProduct_id();
        long create_at = item.getCreate_at().getTime();
        long update_at = item.getUpdate_at().getTime();
        long delete_at = item.getDelete_at().getTime();
        Object[] args={count, bill_id,
                product_id, create_at, update_at, delete_at, id};
        conn.executeSql(itemSQL.updateByBean, args);
        return true;
    }

    @Override
    public int selectMaxId() {
        DBconnection conn = DBconnection.getInstance();
        String sql = itemSQL.selectMaxId;
        Cursor cursor = conn.querySql(sql);
        int maxid = 0;
        if (cursor.moveToNext()) {
            int idIndex = cursor.getColumnIndex("max");
            maxid = cursor.getInt(idIndex);

        }
        cursor.close();
        return maxid;
    }

    @Override
    public boolean existById(int id) {
        DBconnection conn = DBconnection.getInstance();
        String sql = String.format(itemSQL.existById, id);
        Cursor cursor = conn.querySql(sql);
        boolean f = false;
        if (cursor.moveToNext())
            f = true;
        cursor.close();
        return f;
    }

    @Override
    public boolean isDeletedById(int id) {
        DBconnection conn = DBconnection.getInstance();
        String sql = String.format(itemSQL.isDeletedById, id);
        Cursor cursor = conn.querySql(sql);
        boolean f = false;
        if (cursor.moveToNext())
            f = true;
        cursor.close();
        return f;
    }

    @Override
    public List<Bean> selectAllBean() {
        DBconnection conn = DBconnection.getInstance();
        String sql = itemSQL.selectAllBean;
        Cursor cursor = conn.querySql(sql);
        List<Item> itemlist = this.cursorToItemList(cursor);
        List<Bean> beanlist = new ArrayList<Bean>();
        for (Item b : itemlist) {
            beanlist.add(b);
        }
        cursor.close();
        return beanlist;
    }

}
